package com.yonyou.pmclouds.billview.plugin;

import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.BeanHelper;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.businessobject.util.BusinessObjectConsts;
import com.yonyou.pmclouds.enterprise.entity.EnterpriseInfo;
import com.yonyou.pmclouds.outter.enterprise.EnterpriseInfoQuery;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.processinspect.entity.ProcedureInspectBodyVO;
import com.yonyou.pmclouds.processinspect.entity.ProcedureInspectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Component
public class ProceInspectViewPlugin implements IBillViewCustomPlugin {

    @Autowired
    private UserInfoQuery userInfoQuery;
    @Autowired
    private EnterpriseInfoQuery enterpriseInfoQuery;

    @Override
    public Object[] fillListBuziField(Object[] objects) throws BusinessException {
        userInfoQuery.tranUserInfoField(objects, new String[]{CommonFieldConst.CREATOR}, new String[]{ProcedureInspectVO.CREATORNAME});

        // 翻译施工单位
        enterpriseInfoQuery.tranEntInfoField(objects, new String[]{ProcedureInspectVO.PK_CONSTRUCTION_UNIT},
                new String[]{ProcedureInspectVO.CONSTRUCTION_UNIT_NAME});

        return objects;
    }

    @Override
    public Map<String, Object[]> fillDetailBuziField(SuperVO superVO, HttpServletRequest request) throws BusinessException {
        ProcedureInspectVO procedureInspectVO = (ProcedureInspectVO) superVO;

        Map<String, EnterpriseInfo> enterpriseInfoMap = enterpriseInfoQuery
                .queryEnterpriseByIds(new String[]{procedureInspectVO.getPk_constructionunit(), procedureInspectVO.getPk_supervisionunit()});
        if (enterpriseInfoMap != null) {
            EnterpriseInfo enterpriseInfo = enterpriseInfoMap.get(procedureInspectVO.getPk_constructionunit());
            BeanHelper.setProperty(superVO, "constructionunit_name", enterpriseInfo == null ? "" : enterpriseInfo.getName());

            EnterpriseInfo supEnterpriseInfo = enterpriseInfoMap.get(procedureInspectVO.getPk_supervisionunit());
            BeanHelper.setProperty(superVO, "supervisionunit_name", supEnterpriseInfo == null ? "" : supEnterpriseInfo.getName());
        }

        userInfoQuery.tranUserInfoField(new ProcedureInspectVO[]{procedureInspectVO},
                new String[]{ProcedureInspectVO.CREATOR, ProcedureInspectVO.ACCEPTOR},
                new String[]{ProcedureInspectVO.CREATORNAME, ProcedureInspectVO.ACCEPTORNAME});
        userInfoQuery.tranUserInfoField(procedureInspectVO.getBodyVOs(),
                new String[]{ProcedureInspectBodyVO.RECORD_CREATOR},
                new String[]{ProcedureInspectBodyVO.RECORD_CREATOR_NAME});

        Map<String, Object[]> resultMap = new HashMap<>();

        resultMap.put(BusinessObjectConsts.PMBD_PROCEDUREINSPECT, new Object[]{procedureInspectVO});
        resultMap.put(BusinessObjectConsts.SUB_PM_CONSTRUCTIONLOG, ((ProcedureInspectVO) superVO).getConstructionLogVOs());
        return resultMap;
    }

    @Override
    public String getBillType() {
        return BillTypeConst.PROCEDUREINSPECT_BILLTYPE;
    }
}
